#
# config/platform.mk
#
# Set platform-specific variables for building iRODS.  These variables
# include the name of the C compiler, loader, archiver, and ranlib, plus
# standard compile and link flags.  Many of these variables are set
# differently depending upon the current OS platform.
#

#
# Commands as found by the 'configure' script during iRODS installation
# These can be overridden here (discouraged) or by setting similar
# variables in irods.config before running 'configure'.
#
CC=/usr/bin/g++
CC_IS_GCC=1
#CCFLAGS=
LDR=/usr/bin/g++
#LDRFLAGS=
RANLIB=/usr/bin/ranlib
AR=/usr/bin/ar
PERL=/usr/bin/perl

ifdef HPSS
# For now, need to build iRODS with gcc instead of g++ when HPSS is included.
# If the right gcc is not in the path, the lines below may need adjustment.
CC=gcc
LDR=gcc
CC_IS_GCC=1
endif

ifeq ($(OS_platform), aix_platform)
ifdef HPSS
CC=xlc_r -q32
LDR=xlc_r
CC_IS_GCC=0
endif
endif

# added by Jason C (updated by Wayne for solaris)
ifneq ($(findstring solaris, $(OS_platform)),)
# On Solaris, still not using g++ (for now) so can't have no-write-flags
else
CCFLAGS += -Wno-write-strings
endif

# =-=-=-=-=-=-=-
# JMC :: BOOST Include & Linking
ifdef USE_BOOST
CCFLAGS+=-I$(BOOST_DIR)/include
CCFLAGS+=-DUSE_BOOST -DUSE_BOOST_COND -DUSE_BOOST_FS -DBOOST_ENABLE_ASSERT_HANDLER
LDRFLAGS+=-L$(BOOST_DIR)/lib -lboost_system -lboost_thread -lboost_thread-mt -lboost_filesystem -lboost_regex
endif
# =-=-=-=-=-=-=-

# USE_GCOV - specify whether to compile for gcov test coverage
#USE_GCOV = 1
ifdef USE_GCOV
CCFLAGS += -fprofile-arcs -ftest-coverage
LDRFLAGS += -lgcov
endif

ifdef OOI_CI
CCFLAGS += -DOOI_CI -I/usr/include/libxml2
endif

ifdef CCFLAGS
MY_CFLAG += $(CCFLAGS)
endif
ifdef LDRFLAGS
LDADD += $(LDRFLAGS)
CL_LDADD += $(LDRFLAGS)
endif





#
# Special memory allocation library
#
ifdef CCMALLOC
LDADD+=$(CCMALLOC_DIR)/obj/ccmalloc-gcc.o -L$(CCMALLOC_DIR)/lib -lccmalloc -ldl -lstdc++
CL_LDADD+=$(CCMALLOC_DIR)/obj/ccmalloc-gcc.o -L$(CCMALLOC_DIR)/lib -lccmalloc -ldl -lstdc++
endif


#
# Embedded PHP
#
ifdef PHP_LIB
AG_LDADD+=-L$(PHP_LIB_DIR) -lphp5
endif


#
# Ar options
#
AROPT= -crs
ifeq ($(OS_platform), osx_platform)
AROPT= -cr
endif


#
# Compiler and loader options
#
MY_CFLAG+= -g
LDADD+= -lm -lpthread
CL_LDADD+= -lm -lpthread

ifeq ($(OS_platform), solaris_pc_platform)
OS_platform=solaris_platform
hardware_platform=i86_hardware
endif

# 64-bit file addressing ("large files")
ifeq ($(FILE_64BITS), 1)
ifeq ($(OS_platform), solaris_platform)
MY_CFLAG+=-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
endif
ifeq ($(OS_platform), aix_platform)
MY_CFLAG+=-D_LARGE_FILES
endif
ifeq ($(OS_platform), linux_platform)
MY_CFLAG+=-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE
endif
ifeq ($(OS_platform), osx_platform)
MY_CFLAG+=-D_FILE_OFFSET_BITS=64
endif
endif

# 64-bit memory addressing
ifeq ($(ADDR_64BITS), 1)
MY_CFLAG+= -DADDR_64BITS
endif

# Parallel operations
ifdef PARA_OPR
MY_CFLAG+= -DPARA_OPR=1
endif

# New rule engine
ifdef RULE_ENGINE_N
MY_CFLAG+= -DRULE_ENGINE_N=1
endif

# Hardware choices
MY_CFLAG+= -D$(OS_platform)
ifdef hardware_platform
MY_CFLAG+= -D$(hardware_platform)
endif

ifeq ($(CC_IS_GCC), 1)
MY_CFLAG+=-Wall
GCC_VERSION_GE_46 := $(shell expr `g++ -dumpversion | cut -c1-3` \>= 4.6)
ifeq ($(GCC_VERSION_GE_46),1)
MY_CFLAG+=-Wno-unused-but-set-variable
endif
endif

# Alpha
ifeq ($(OS_platform), alpha_platform)
MY_CFLAGS+=-pthread  -std0 -verbose
endif

# Sun Solaris
ifeq ($(OS_platform), solaris_platform)
ifeq ($(ADDR_64BITS), 1)
ifeq ($(CC_IS_GCC), 0)
MY_CFLAG+=-xarch=v9
endif
endif
LDADD+=-lnsl -lsocket -lm -lpthread -lrt
CL_LDADD+=-lnsl -lsocket -lm -lpthread
endif

# Authorization
ifdef GSI_AUTH
ifeq ($(OS_platform), aix_platform)
LDADD+= $(LIB_GSI_AUTH) $(KRB_LIBS)
CL_LDADD+= $(LIB_GSI_AUTH) $(KRB_LIBS)
else
ifeq ($(OS_platform), osx_platform)
LDADD+= $(LIB_GSI_AUTH) $(KRB_LIBS)
CL_LDADD+= $(LIB_GSI_AUTH) $(KRB_LIBS)
else
LDADD+= $(LIB_GSI_AUTH) $(KRB_LIBS) -z muldefs
CL_LDADD+= $(LIB_GSI_AUTH) $(KRB_LIBS) -z muldefs
endif
endif
endif

ifdef HAAW_STRUCT_FILE
MY_CFLAG+= -DHAAW_STRUCT_FILE
endif

ifdef MSSO_STRUCT_FILE
MY_CFLAG+= -DMSSO_STRUCT_FILE
endif

ifdef TAR_STRUCT_FILE
MY_CFLAG+= -DTAR_STRUCT_FILE
ifdef TAR_EXEC_PATH
ifdef GNU_TAR
MY_CFLAG+= -DGNU_TAR
endif
MY_CFLAG+= -DTAR_EXEC_PATH=\"$(TAR_EXEC_PATH)\"
else
MY_CFLAG+= -I$(tarDir) -I$(tarDir)/listhash \
                  -I${tarDir}/lib \
                  -I${tarDir}/compat
endif
ifdef TAR_EXTENDED_HDR
MY_CFLAG+=  -DTAR_EXTENDED_HDR
endif
endif

ifdef ZIP_EXEC_PATH
MY_CFLAG+= -DZIP_EXEC_PATH=\"$(ZIP_EXEC_PATH)\"
endif

ifdef UNZIP_EXEC_PATH
MY_CFLAG+= -DUNZIP_EXEC_PATH=\"$(UNZIP_EXEC_PATH)\"
endif

ifdef RBUDP_TRANSFER
MY_CFLAG+= -DRBUDP_TRANSFER -I$(libCoreIncDir) -I$(libApiIncDir)        \
		-I$(libMd5IncDir) -I$(libSha1IncDir) -I$(libRbudpIncDir)
endif

ifdef UNI_CODE
MY_CFLAG+= -DUNI_CODE
endif

ifdef COMPAT_201
MY_CFLAG+= -DCOMPAT_201
endif

# server specific LDADD

ifdef TAR_STRUCT_FILE
ifndef TAR_EXEC_PATH
LDADD+=-L$(tarDir)/lib -ltar
endif
endif

# HPSS support
ifdef HPSS
MY_CFLAG+= -DHPSS
ifdef HPSS7
MY_CFLAG+= -DHPSS7
endif
ifdef HPSS_UNIX_PASSWD_AUTH
MY_CFLAG+= -DHPSS_UNIX_PASSWD_AUTH
else
ifdef HPSS_KRB5_AUTH
MY_CFLAG+= -DHPSS_KRB5_AUTH
endif
endif
ifeq ($(OS_platform), linux_platform)
ifdef ADDR_64BITS
MY_CFLAG+= -Duchar=u_char -DLITTLEEND
else
MY_CFLAG+= -m32 -Duchar=u_char -DLITTLEEND
LDADD+= -m32
CL_LDADD+= -m32
endif
# XXXX looks like the hpss lib is in 32 bit. ADDR_64BITS in config.mk should
# be undefined too
endif
# have to add -lpthread here for ldr to work even have this before
LDADD+=-L$(HPSS_LIB_DIR) -lhpss -lpthread
ifeq ($(OS_platform), solaris_platform)
MY_CFLAG+= -I$(HPSS_HDR_DIR) -DNO_DCE -DNO_H_ERRNO -Dsolaris -Dsolaris_2_5 \
        -DTHREADS_ENABLED -DPTHREADS
else
# XXXX assume linux for now
MY_CFLAG+= -I$(HPSS_HDR_DIR) -DNO_DCE -DNO_H_ERRNO -Dlinux \
        -DTHREADS_ENABLED -DPTHREADS
endif
endif

ifdef AMAZON_S3
MY_CFLAG+= -DAMAZON_S3 -I$(S3_HDR_DIR)
LDADD+=-L$(S3_LIB_DIR) -ls3 -lcurl -lxml2
endif

ifdef HDFS
MY_CFLAG+= -DHDFS
endif

ifdef DDN_WOS
MY_CFLAG+= -DDDN_WOS -I$(WOS_DIR)/include
LDADD+=-L$(WOS_DIR)/lib64 -lwos_cpp
endif

ifdef NETCDF4_API
MY_CFLAG += -DNETCDF4_API
endif

ifdef NETCDF_API
MY_CFLAG += -DNETCDF_API -I$(NETCDF_DIR)/include
LDADD += -L$(NETCDF_DIR)/lib -lnetcdf -lcurl
CL_LDADD += -L$(NETCDF_DIR)/lib -lnetcdf
ifdef LIB_CF
MY_CFLAG += -DLIB_CF -I$(LIB_CF_INC_DIR)
LDADD += -L$(LIB_CF_LIB_DIR) -lcf_src
endif
endif

ifdef NETCDF_CLIENT
MY_CFLAG += -DNETCDF_CLIENT
endif

ifdef PYDAP
MY_CFLAG += -DPYDAP
endif

ifdef TDS
MY_CFLAG += -DTDS -I$(LIB_XML2_INC_DIR)
LDADD += -L$(LIB_XML2_LIB_DIR) -lxml2
endif

ifdef ERDDAP
MY_CFLAG += -DERDDAP -I$(LIB_JANSSON_INC_DIR)
LDADD += -L$(LIB_JANSSON_LIB_DIR) --ljansson
endif

ifdef RUN_SERVER_AS_ROOT
MY_CFLAG+= -DRUN_SERVER_AS_ROOT
endif

ifdef OS_AUTH
MY_CFLAG+= -DOS_AUTH -DOS_AUTH_KEYFILE=\"$(OS_AUTH_KEYFILE)\"
ifdef OS_AUTH_CMD
MY_CFLAG+= -DOS_AUTH_CMD=\"$(OS_AUTH_CMD)\"
endif
ifdef OS_AUTH_NO_UID
MY_CFLAG+= -DOS_AUTH_NO_UID
endif
endif

ifdef FILESYSTEM_META
MY_CFLAG+= -DFILESYSTEM_META
endif

ifdef DIRECT_ACCESS_VAULT
MY_CFLAG+= -DDIRECT_ACCESS_VAULT
endif

ifdef DEF_CONFIG_DIR
MY_CFLAG+= -DDEF_CONFIG_DIR=\"$(DEF_CONFIG_DIR)\"
endif
ifdef DEF_STATE_DIR
MY_CFLAG+= -DDEF_STATE_DIR=\"$(DEF_STATE_DIR)\"
endif
ifdef DEF_LOG_DIR
MY_CFLAG+= -DDEF_LOG_DIR=\"$(DEF_LOG_DIR)\"
endif
ifdef CMD_DIR
MY_CFLAG+= -DCMD_DIR=\"$(CMD_DIR)\"
endif

ifdef PAM_AUTH_CHECK_PROG
MY_CFLAG+= -DPAM_AUTH_CHECK_PROG=\"$(PAM_AUTH_CHECK_PROG)\"
endif

ifdef OOI_CI
LDADD+= -ljansson
CL_LDADD+= -ljansson
endif

ifdef STORAGE_ADMIN_ROLE
MY_CFLAG+= -DSTORAGE_ADMIN_ROLE
endif

ifdef SHA256_FILE_HASH
ifdef PREFER_SHA256_FILE_HASH
MY_CFLAG+= -DPREFER_SHA256_FILE_HASH=$(PREFER_SHA256_FILE_HASH)
endif
MY_CFLAG+= -DSHA256_FILE_HASH
MY_CFLAG+= -I$(SHA256_INC)
LDADD+= -l$(SHA256_LIB)
CL_LDADD+= -l$(SHA256_LIB)
endif
